#    Tichy
#
#    copyright 2008 Guillaume Chereau (charlie137@gmail.com)
#
#    This file is part of Tichy.
#
#    Tichy is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Tichy is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Tichy.  If not, see <http://www.gnu.org/licenses/>.


from widget import Widget
from tichy.object import Object
from geo import Rect, Vect
import wrap


class Label(Widget):

    def __init__(self, parent, text ='', multilines=False, **kargs):
        import tichy
        super(Label, self).__init__(parent, **kargs)
        self.font = self.style.font or tichy.Font()
        self.multilines = multilines
        self.text = unicode(text)
        self.store_surface = True

    def compute_sizes(self):
        return

    def __get_text(self):
        return self.__text

    def __set_text(self, value):
        assert isinstance(value, basestring)
        self.__text = unicode(value)
        if not self.multilines:
            size = self.font.size(self.__text)
            self.optimal_size = Vect(size[0] + 16, size[1] + 16)
        else:
            size = wrap.get_size(self.__text, self.font, self.size[0])
            self.optimal_size = size
        self.min_size = Vect(0, 40)
        self.need_redraw(self.rect)

    text = property(__get_text, __set_text)

    def draw(self, painter):
        super(Label, self).draw(painter)
        if not self.__text:
            return
        painter.move(Vect(8, 8))
        color = self.style.font_color or (255, 255, 255)
        if not self.multilines:
            painter.draw_text(self.__text, self.font, color)
        else:
            wrap.draw(painter, self.__text, self.font, color,
                      self.size.x - 16)
        painter.umove(Vect(8, 8))
